<template>
  <el-dialog
    :close-on-click-modal="false"
    :show-close="false"
    :title="isAdd?'新增供应商':'修改供应商'"
    :visible.sync="isEdit"
    width="30%"
  >
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="form"
      label-width="120px"
      size="small"
      class="form-box"
    >
      <el-form-item label="供应商名称" prop="name">
        <el-input type="text" v-model="form.name" placeholder="请输入供应商名称"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="username">
        <el-input type="text" v-model="form.username" placeholder="请输入联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input  v-model="form.phone" maxlength="11" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item label="渠道商分类" >
        <el-select v-model="form.cate_id" clearable placeholder="请选择">
          <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请求域名" prop="domain">
        <el-input  v-model="form.domain" placeholder="请输入请求域名"></el-input>
      </el-form-item>
      <el-form-item label="请求账号" prop="mch_id">
        <el-input  v-model="form.mch_id" placeholder="请输入请求账号"></el-input>
      </el-form-item>
      <el-form-item label="请求Key" prop="key">
        <el-input  v-model="form.key" placeholder="请输入请求Key"></el-input>
      </el-form-item>
     
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('closeDialog')" size="medium">取消</el-button>
      <el-button @click="submitFormAdd('ruleForm')" type="primary" size="medium">确定</el-button>
    </span>
  </el-dialog>
</template>

<script lang="ts">
let id = 0;
import { Component, Vue, Prop, Provide, Watch } from "vue-property-decorator";
import supplierApi from "@/api/supplier";
import isMobileNumber from "@/api/formValidation.js";
import commonApi from "@/api/common";
import Region from "@/components/region/index.vue";
@Component({
  components: {Region}
})
export default class supplierEditDialog extends Vue {
  // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
  check:boolean = true//防重复提交
  @Prop(Boolean) isEdit!: boolean;
  @Prop(Boolean) isAdd!: boolean;    
  @Prop(Object) form!: {
    id:number,
    name: string,
    username: string,
    phone:string,
    cate_id:number,
    domain:string,
    mch_id:string,
    key:string,
  };
  @Provide() add: any=true;
  @Provide() timer: any='';
  @Provide() rules: any = {
    name: [{ required: true, message: "请输供应商名称", trigger: "blur" }],
    username: [{ required: true, message: "请输入联系人", trigger: "blur" }],
    phone: [{ required: true, message: "请输入联系电话", trigger: "blur" },{ validator: isMobileNumber, trigger: "blur" }],
  };
  @Provide() typeList: any = [];
  // 提交
  submitFormAdd(formName: any) {
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        if(this.check) {
          let _this = this
          _this.check = false
          supplierApi.saveSupplier(this.form)
          .then((res: any) => {
            if(res.data.data==1){
              _this.$message({
                message: '操作成功',
                type: "success"
              });
              _this.$emit("closeDialog");
              _this.$emit("loadData")
              setTimeout(function(){
                _this.check = true
              },1500)
            }else{
              _this.$message({
                message: res.data.msg,
                type: "error"
              });
              _this.check = true
            }
          });
        }
          
      }
    });
  }
  // 获取权限组列表
  typeData (){        
    this.typeList = [{id:0,name:'自营'},{id:10,name:'自我游'}];     
  }

  created() {
    this.typeData();
  }
}
</script>

<style lang="scss" scoped>
.form-box {
  .el-input,
  .el-select {
    width: 200px !important;
  }
}
</style>
